import Vue from 'vue'
import store from '@/store'
import VueRouter from 'vue-router'

// 一级路由
import Login from '@/views/login'
import Layout from '@/views/layout'

// 二级路由
import Data from '@/views/data'
import BusinessVue from '@/views/task/BusinessVue'
import OperationVue from '@/views/task/OperationVue'
import Region from '@/views/manage/region'
import Point from '@/views/manage/point'
import Partner from '@/views/manage/Partner'
import devicegl from '@/views/device/devicegl'
import devicezt from '@/views/device/dvvicezt'
import deviceType from '@/views/device/deviceType'
import workloadList from '@/views/personnel/workloadList'
import personnelList from '@/views/personnel/personnelList'
import personnelStati from '@/views/personnel/personnelStati'
import typesof from '@/views/commodity/typesOf'
import manage from '@/views/commodity/manage'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/login', component: Login },
    {
      path: '/',
      component: Layout,
      redirect: '/data',
      children: [
        { path: 'data', component: Data },
        { path: 'business', component: BusinessVue },
        { path: 'operation', component: OperationVue },
        { path: 'region', component: Region },
        { path: 'point', component: Point },
        { path: 'partner', component: Partner },
        { path: 'devicegl', component: devicegl },
        { path: 'devicezt', component: devicezt },
        { path: 'devicetype', component: deviceType },
        { path: 'workloadlist', component: workloadList },
        { path: 'personnelList', component: personnelList },
        { path: 'personnelStati', component: personnelStati },
        { path: 'typesof', component: typesof },
        { path: 'manage', component: manage }
      ]
    }
  ]
})
// 全局前置路由守卫
const whiteList = ['/login']
// 全局前置路由守卫
router.beforeEach(async (to, from, next) => {
  if (store.getters.token) {
    // 存在token
    if (to.path === '/login') {
      // 跳转到主页
      next('/') // 中转到主页
    } else {
      next() // 放行
    }
  } else {
    // 没有token
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login') // 中转到登录页
    }
  }
})
// 后置守卫
router.afterEach(() => {
})
export default router
